package com.example.hibernate.demo.controller;

import com.example.hibernate.demo.entity.*;
import com.example.hibernate.demo.repository.*;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 这个控制器展示了更多Hibernate的高级功能
 */
@RestController
@RequestMapping("/api/hibernate-examples")
@RequiredArgsConstructor
public class HibernateExamplesController2 {

    @PersistenceContext
    private EntityManager entityManager;
    
    /**
     * 展示HQL查询
     */
    @GetMapping("/hql-demo")
    @Transactional(readOnly = true)
    public ResponseEntity<Map<String, Object>> hqlDemo() {
        Map<String, Object> result = new HashMap<>();
        
        // 基本HQL查询
        TypedQuery<Employee> query1 = entityManager.createQuery(
                "SELECT e FROM Employee e WHERE e.salary > :minSalary", Employee.class);
        query1.setParameter("minSalary", new BigDecimal("70000.00"));
        List<Employee> highPaidEmployees = query1.getResultList();
        result.put("highPaidEmployees", highPaidEmployees.size());
        
        // 连接查询
        TypedQuery<Object[]> query2 = entityManager.createQuery(
                "SELECT e.firstName, e.lastName, d.name FROM Employee e JOIN e.department d", Object[].class);
        List<Object[]> employeesWithDepartments = query2.getResultList();
        result.put("employeesWithDepartments", employeesWithDepartments.size());
        
        // 聚合查询
        TypedQuery<Object[]> query3 = entityManager.createQuery(
                "SELECT d.name, COUNT(e), AVG(e.salary) FROM Department d JOIN d.employees e GROUP BY d.name", Object[].class);
        List<Object[]> departmentStats = query3.getResultList();
        result.put("departmentStats", departmentStats.size());
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 展示Criteria API
     */
    @GetMapping("/criteria-demo")
    @Transactional(readOnly = true)
    public ResponseEntity<Map<String, Object>> criteriaDemo() {
        Map<String, Object> result = new HashMap<>();
        
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        
        // 基本Criteria查询
        CriteriaQuery<Employee> query1 = cb.createQuery(Employee.class);
        Root<Employee> employeeRoot = query1.from(Employee.class);
        query1.select(employeeRoot);
        Predicate salaryPredicate = cb.greaterThan(employeeRoot.get("salary"), new BigDecimal("70000.00"));
        query1.where(salaryPredicate);
        List<Employee> highPaidEmployees = entityManager.createQuery(query1).getResultList();
        result.put("highPaidEmployees", highPaidEmployees.size());
        
        // 连接查询
        CriteriaQuery<Object[]> query2 = cb.createQuery(Object[].class);
        Root<Employee> empRoot = query2.from(Employee.class);
        query2.multiselect(
                empRoot.get("firstName"),
                empRoot.get("lastName"),
                empRoot.get("department").get("name")
        );
        List<Object[]> employeesWithDepartments = entityManager.createQuery(query2).getResultList();
        result.put("employeesWithDepartments", employeesWithDepartments.size());
        
        return ResponseEntity.ok(result);
    }
}